package com.sugon.controller.device;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.sugon.annotation.AutoLog;
import com.sugon.dao.InspectionRecordingJoinDao;
import com.sugon.entity.DeviceInfoEntity;
import com.sugon.entity.InspectionRecordingJoinEntity;
import com.sugon.modules.device.consts.DeviceStatus;
import com.sugon.service.DeviceInfoService;
import com.sugon.service.InspectionRecordingJoinService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 巡检记录选项关联Controller
 *
 * @author YuXD
 * @date 2020-12-03 16:46
 */
@Controller
@RequestMapping("inspectionrecordingjoin")
public class InspectionRecordingJoinController {

    @Autowired
    private InspectionRecordingJoinService inspectionRecordingJoinService;
    @Autowired
    private InspectionRecordingJoinDao inspectionRecordingJoinDao;
    @Autowired
    private DeviceInfoService deviceInfoService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<Map<String, Object>> inspectionRecordingJoinList = inspectionRecordingJoinService.queryListByDEV(query);
        int total = inspectionRecordingJoinService.queryTotalByDEV(query);

        PageUtils pageUtil = new PageUtils(inspectionRecordingJoinList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        InspectionRecordingJoinEntity inspectionRecordingJoin = inspectionRecordingJoinService.queryObject(id);

        return R.ok().put("inspectionRecordingJoin", inspectionRecordingJoin);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @ResponseBody
    public R save(@RequestBody InspectionRecordingJoinEntity inspectionRecordingJoin) {
        // 设备编码必填
        String devId = inspectionRecordingJoin.getDevId();
        if (StrUtil.isEmpty(devId)) {
            return R.error("设备编码必填！");
        }
        // 校验设备编码是否非法
        DeviceInfoEntity deviceInfo = deviceInfoService.queryObject(devId);
        if (deviceInfo == null) {
            return R.error("非法设备编码！");
        }
        // 对于闲置或闲置审核中的设备，无需进行设备巡检
        if (DeviceStatus.LEAVE.equals(deviceInfo.getDevStatus()) || DeviceStatus.LEAVE_APPROVING.equals(deviceInfo.getDevStatus())) {
            return R.error("闲置或闲置审核中设备无需进行设备巡检！");
        }
        return inspectionRecordingJoinService.save(inspectionRecordingJoin) > 0 ? R.ok() : R.error();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @ResponseBody
    public R update(@RequestBody InspectionRecordingJoinEntity inspectionRecordingJoin) {
        inspectionRecordingJoinService.update(inspectionRecordingJoin);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        inspectionRecordingJoinService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<InspectionRecordingJoinEntity> list = inspectionRecordingJoinService.queryList(params);

        return R.ok().put("list", list);
    }


    /**
     * 获取巡检日志记录，返回1,2,3日期 用于判断日历数据
     *
     * @param params
     * @return
     */
    @RequestMapping("loadDay")
    @ResponseBody
    public R loadDay(@RequestParam Map<String, Object> params) {
        String devId = MapUtil.getStr(params, "devId");
        Integer year = MapUtil.getInt(params, "year");
        Integer month = MapUtil.getInt(params, "month");
        // 获取设备指定月份内的巡检时间列表
        List<Date> planCarryDateList = inspectionRecordingJoinDao.queryPlanCarryDate(devId, year, month);
        String result = "";
        Set<Integer> set = new HashSet<>();
        if (CollectionUtil.isNotEmpty(planCarryDateList)) {
            for (Date planCarryDate : planCarryDateList) {
                int day = DateUtil.dayOfMonth(planCarryDate);
                if (set.add(day)) {
                    result += "," + DateUtil.dayOfMonth(planCarryDate);
                }
            }
            if (StrUtil.isNotEmpty(result)) {
                result = result.replaceFirst(",", "");
            }
        }
        return R.ok().put("result", result);
    }

}
